$(function () {
    var querystring = window.location.search;
    var nodeidx = (querystring.match(/node=(\w+)/))[1];
    var ZZJGNODE;
    var INFO = window.NODE[nodeidx];
    var addFrameTab = function (title, url) {
        window.parent.addFrameTab(title, url);
    };
    $("#divTitle").text(INFO.title);
    $(window).resize(function () {
        $("#ttWrap").css({
            height: ($(window).innerHeight() - $("#ttWrap").offset().top - INFO.queryBottom) + 'px',
            width: (($(window).innerWidth() - 30) + 'px')
        });
    }).resize();
    var query = function (ZZJGID) {
        var list = [];
        for (var index = 0; index < 100; index++) {
            list.push({ ID: index,
                GJMC: "xxx", GSMC: "xxx", XMMC: "xxx" + index,
                XMZT: "xxx", YZ: "xxx", CBS: "xxx",
                JSGM: "xxx", JHZTZE: "xxx", SJZTZE: "xxx",
                JHKGRQ: "xxx", JHGQ: "xxx", SJKGRQ: "xxx", SJGQ: "xxx"
            });
        }
        $("#tt").autoTable($.extend({ data: list }, INFO.tblOpt));
    };
    $(window).on("message", function (event) {
        ZZJGNODE = parent["CurTreeNode"];
        query(ZZJGNODE.ID);
    });
    ZZJGNODE = parent["CurTreeNode"];
    query(ZZJGNODE.ID);
    var getSelectedRow = function () {
        var $sel = $("#tt").find(".autoselected");
        if ($sel.length == 0) {
            $.auto.alert("提示", "请先在数据表格中选择操作的行！");
            return false;
        }
        else {
            var ret_1 = {};
            _.each(INFO.tblOpt.keydataOnRow, function (key) {
                ret_1[key] = $sel.data(key.toLowerCase());
            });
            return ret_1;
        }
    };
    $("#btnAdd").click(function () {
        addFrameTab("新增", "SIPCAddEdit.html?id=&node=" + nodeidx);
    });
    $("#btnEdit").click(function () {
        var keyData = getSelectedRow();
        if (keyData) {
            addFrameTab("编辑", "SIPCAddEdit.html?id=" + keyData["ID"] + "&node=" + nodeidx);
        }
    });
    $("#btnDel").click(function () {
        var keyData = getSelectedRow();
        if (keyData) {
            var hint = (_.template(INFO.deleteHint))(keyData);
            $.auto.confirm("提示", hint, function () {
                $.ajax(INFO.deleteUrl, { type: 'POST', data: { id: keyData["ID"] }, dataType: 'json' });
            });
        }
    });
    $("#btnDetails").click(function () {
        var keyData = getSelectedRow();
        if (keyData) {
        }
    });
    $("#btnFolder").click(function () {
        var keyData = getSelectedRow();
        if (keyData) {
            addFrameTab("详细信息", "SIPCFolder.html?id=" + keyData["ID"] + "&node=" + nodeidx);
        }
    });
    $("#btnJQCZ").click(function () {
        $("#jqczForm").autoForm(INFO.jqczOpt);
        $("#jqczWrap").dialog({
            title: "精确查询",
            width: "500px"
        });
    });
    $("#btnJqczSearch").click(function () {
        var condition = {};
        var $form = $("#jqczForm");
        _.each(INFO.jqczOpt.formFields, function (item) {
            condition[item.field] = $form.find("[name=" + item.field + "]").val();
        });
        var opt = ($("#tt").autoTable("option")._opt);
        var filter = function (row) {
            var flag = true;
            _.each(INFO.jqczOpt.formFields, function (item) {
                if (row[item.field].indexOf(condition[item.field]) == -1) {
                    flag = false;
                }
            });
            return flag;
        };
        opt.filter = filter;
        opt.pageNumber = 1;
        $("#tt").autoTable(opt);
    });
    $("#btnJqczClose").click(function () {
        $("#jqczWrap").dialog("close");
    });
});
